package cn.mygweb.admin.modules.sys.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;

import cn.mygweb.admin.modules.sys.mapper.MenuMapper;
import cn.mygweb.admin.modules.sys.pojo.Menu;
import cn.mygweb.admin.modules.sys.service.MenuService;
import cn.mygweb.common.utils.TreeUtil;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/** 
* <p>Title: SysMenuServiceImpl.java</p>
* <p>Description: 系统菜单</p>  
* @author gxb 
* @date 2019年6月2日 
* <p>Copyright: Copyright (c) 2019</p>
* <p>Company: www.mygweb.cn</p> 
*/
@Service
public class MenuServiceImpl implements MenuService {

	@Autowired
	private MenuMapper menuMapper;

	/**
	 * <p>Title: queryMenuList</p>
	 * <p>Description: 查询所有的菜单信息</p>
	 * @param sysMenu
	 * @return
	 * @see cn.mygweb.admin.modules.sys.service.MenuService#queryMenuList(cn.mygweb.admin.modules.sys.pojo.Menu)
	 */
	@Override
	@Transactional(propagation=Propagation.SUPPORTS)
	public List<Menu> queryMenuList(Menu menu) {
		Example example = new Example(Menu.class);
//		Example.Criteria criteria = example.createCriteria();
//		criteria.setAndOr("order by sort asc");
//		if(!StringUtils.isEmptyOrWhitespace(menu.getName())) {
//			criteria.andLike("name", "%" + menu.getName() + "%");
//		}
		example.setOrderByClause("sort asc");//按照序号进行升序排序
		return menuMapper.selectByExample(example);
	}

	/**
	 * <p>Title: save</p>
	 * <p>Description: 添加菜单信息</p>
	 * @param menu
	 * @see cn.mygweb.admin.modules.sys.service.MenuService#save(cn.mygweb.admin.modules.sys.pojo.Menu)
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void save(Menu menu) {
		menuMapper.insert(menu);
	}

	/**
	 * <p>Title: deleteById</p>
	 * <p>Description: 通过id删除菜单信息</p>
	 * @param id
	 * @see cn.mygweb.admin.modules.sys.service.MenuService#deleteById(java.lang.String)
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void deleteById(String id) {
		//删除子项记录，根据具体业务需要看是否需要直接删除这些记录
		//问题：目前只能往下删一级，如果是多级菜单，那么再往下级别的菜单还是无法删除，除非用递归
		Example example = new Example(Menu.class);
		Criteria criteria = example.createCriteria();
		if(!StringUtils.isEmptyOrWhitespace(id)) {
			criteria.orCondition("parent_id='" + id + "'");
		}
		menuMapper.deleteByExample(example);
		
		//删除本条记录
		menuMapper.deleteByPrimaryKey(id);
	}

	/**
	 * <p>Title: update</p>
	 * <p>Description: 修改菜单信息</p>
	 * @param menu
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void update(Menu menu) {
		menuMapper.updateByPrimaryKeySelective(menu);
	}

	/**
	 * <p>Title: queryMenuTree</p>
	 * <p>Description: 查询树状结构数据，包括id，pid，name</p>
	 * @param menu
	 * @return
	 * @see cn.mygweb.admin.modules.sys.service.MenuService#queryMenuTree(cn.mygweb.admin.modules.sys.pojo.Menu)
	 */
	@Override
	@Transactional(propagation=Propagation.SUPPORTS)
	public List<Menu> getMenuTree(Menu menu) {
		Example example = new Example(Menu.class);
		example.selectProperties("id","parentId","name");
		example.setOrderByClause("sort asc");
		return menuMapper.selectByExample(example);
	}

	/**
	 * <p>Title: getRoleMenus</p>
	 * <p>Description: 通过角色id查询对应的菜单信息</p>
	 * @param roleId
	 * @return
	 * @see cn.mygweb.admin.modules.sys.service.MenuService#getRoleMenus(java.lang.String)
	 */
	@Override
	@Transactional(propagation=Propagation.SUPPORTS)
	public List<Menu> getRoleMenus(String roleId) {
		return menuMapper.getRoleMenus(roleId);
	}

	/**
	 * <p>Title: getSidebarMenuTree</p>
	 * <p>Description: 将获取的结果集转换为树状结构的结果集</p>
	 * @param userId
	 * @param menu
	 * @return
	 * @see cn.mygweb.admin.modules.sys.service.MenuService#getSidebarMenuTree(java.lang.String, cn.mygweb.admin.modules.sys.pojo.Menu)
	 */
	@Override
	@Transactional(propagation=Propagation.SUPPORTS)
	public List<Menu> getSidebarMenuTree(String userId, Menu menu) {
		//1.查询所有菜单信息
//		Example example = new Example(Menu.class);
//		example.setOrderByClause("sort asc");
//		Criteria criteria = example.createCriteria();
//		criteria.andCondition("type!=2");//筛选所有的目录和菜单，排除操作选项
//		List<Menu> menuList = menuMapper.selectByExample(example);
		
		//2.查询用户拥有的菜单信息
		List<Menu> menuList = menuMapper.getUserMenus(userId);
		
		return TreeUtil.toTree(menuList, "id", "parentId", "children", Menu.class);
	}

	/**
	 * <p>Title: getUserMenus</p>
	 * <p>Description: 通过用户id查询对应的菜单信息</p>
	 * @param userId
	 * @return
	 * @see cn.mygweb.admin.modules.sys.service.MenuService#getUserMenus(java.lang.String)
	 */
	@Override
	@Transactional(propagation=Propagation.SUPPORTS)
	public List<Menu> getUserMenus(String userId) {
		return menuMapper.getUserMenus(userId);
	}
}
